package com.mpyy.handler.inventory;

import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.DepotInfo;
import com.mpyy.pojo.InventoryCheck;
import com.mpyy.pojo.InventoryCheckDesc;
import com.mpyy.pojo.InventoryDesc;
import com.mpyy.service.inventory.InventoryCheckServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description InventoryCheckController
 * @Author wx
 * @Date 2022-03-07 11:04
 * @Version 1.0
 */
@Controller
public class InventoryCheckController {
    @Autowired
    @Qualifier(value = ("inventoryCheckServiceImpl"))
    private InventoryCheckServiceInter checkServiceInter;
    @RequestMapping("/selectCheckProcess")
    @ResponseBody
    public String selectCheckProcess(HttpServletRequest req){
        String ck = req.getParameter("ck");
        List <InventoryDesc> inventoryDescs = checkServiceInter.searchCheck(ck);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryDescs.size());
        checkHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/selectCheckIdProcess")
    @ResponseBody
    public String selectCheckIdProcess(HttpServletRequest req){
        String id=req.getParameter("Id");
        List <InventoryDesc> inventoryDescs = checkServiceInter.searchCheckId(id);
        HashMap <Object, Object> objectObjectHashMap = new HashMap <>();
        return JSON.toJSONString(inventoryDescs);
    }
    @RequestMapping("/selectTakeStock")
    @ResponseBody
    public String selectTakeStock(){
        List <InventoryCheck> inventoryDescs = checkServiceInter.searchTakeStock();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryDescs.size());
        checkHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/selectInventoryCheckDesc")
    @ResponseBody
    public String selectInventoryCheckDesc(HttpServletRequest req){
        String checkid=req.getParameter("checkid");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(checkid);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryCheckDescs.size());
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
//    查询盘点单
    @RequestMapping("/selectselectInventorySurplus")
    @ResponseBody
    public String selectselectInventorySurplus(){
        List <InventoryCheck> inventoryChecks = checkServiceInter.searchInventorySurpluslosses();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryChecks.size());
        checkHashMap.put("data",inventoryChecks);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/deleteInventoryCheckList")
    @ResponseBody
    public void deleteInventoryCheckList(HttpServletRequest req){
        String checkid=req.getParameter("checkid");
        int i = checkServiceInter.updateInventory(checkid);
        int i1 = checkServiceInter.updateInventoryList(checkid);
    }
    //删除商品明细单
    @RequestMapping("/deleteInventoryCommodity")
    @ResponseBody
    public void deleteInventoryCommodity(HttpServletRequest req){
        String invCheckDescId=req.getParameter("invCheckDescId");
        String checkid=req.getParameter("checkid");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(checkid);
        int a = inventoryCheckDescs.size();
        int i = checkServiceInter.updateCommodity(invCheckDescId);
        if(a==1){
            int il = checkServiceInter.updateInventoryList(checkid);
        }
    }
    List<String> arrty = new ArrayList <>();
    //选择完盘点单后的所有的商品
    @RequestMapping(value = "/selectSecondStep" , produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectSecondStep(HttpServletRequest req) throws Exception {
        String array = req.getParameter("invId");
        String[] arr=array.split(",");
        List<String> stringList = new ArrayList <>();
        for (String s : arr) {
            stringList.add(s);
        }
        arrty=stringList;
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchInventoryDesc(stringList);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",2);
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping(value = "/selectTreeStep", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectTreeStep(){
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchInventoryDesc(arrty);
        for(InventoryCheckDesc o : inventoryCheckDescs){
            int i = o.getInvCheckDescCheckNum() - o.getInvCheckDescInventoryNum();
            o.setQuantityVariance(i);
        }
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",2);
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
String invId;
    @RequestMapping("/updateInvencheckDesc")
    @ResponseBody
    public void updateInvencheckDesc(HttpServletRequest req){
        String inveDescBatchNum=req.getParameter("inveDescBatchNum");
        String invChId=req.getParameter("invChId");
        String pdnum=req.getParameter("pdnum");
        Date date = new Date();

        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddmmHHmmss");
        //获取String类型的时间
        String createdate = sdf.format(date);
        invId=invChId;
        List <InventoryDesc> inventoryDescs = checkServiceInter.searchProidDesc(inveDescBatchNum);
        for(InventoryDesc obj : inventoryDescs){
            InventoryCheckDesc inventoryCheckDesc = new InventoryCheckDesc();
            inventoryCheckDesc.setInvCheckDescId(createdate);
            inventoryCheckDesc.setInvCheckId(invChId);
            inventoryCheckDesc.setProId(obj.getProId());
            inventoryCheckDesc.setInvCheckDescProName(obj.getInveDescName());
            inventoryCheckDesc.setInvCheckDescProType(obj.getInveDescType());
            inventoryCheckDesc.setInvCheckDescProUnit(obj.getInveDescUnit());
            inventoryCheckDesc.setInvCheckDescModel(obj.getInveDescSize());
            inventoryCheckDesc.setInvCheckDescManufacturer(obj.getInveDescManufacturer());
            inventoryCheckDesc.setInvCheckDescInventoryNum(obj.getInveDescInventoryNum());
            inventoryCheckDesc.setInvCheckDescCheckNum(Integer.valueOf(pdnum));
            inventoryCheckDesc.setInvCheckDescBatchNum(obj.getInveDescBatchNum());
            int i = checkServiceInter.updateInventoryCheckDesc(inventoryCheckDesc);
        }
    }
    int count;
    Integer b = 0;
    @RequestMapping("/sraechSta")
    @ResponseBody
    public String sraechSta(HttpServletRequest req){
        String invChId=req.getParameter("invChId");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(invChId);
        //得出盘点单的商品数量
        for(int i=0;i<inventoryCheckDescs.size()-1;i++){
            Integer a= inventoryCheckDescs.get(i).getInvCheckDescCheckNum();
            b+=a;
        }
        count=inventoryCheckDescs.size();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryCheckDescs.size());
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/updateCheckDescIdupdateCheckDescId")
    @ResponseBody
    public void updateCheckDescId(HttpServletRequest req){
        String id=req.getParameter("id");
        int i = checkServiceInter.updateCheckDescId(id);
    }
    //查询仓库
    @RequestMapping("/searchDepotInfo")
    @ResponseBody
    public String searchDepotInfo() {
        List <DepotInfo> depotInfos = checkServiceInter.searchDepotInfowx();
        for(DepotInfo o : depotInfos){
            System.out.println("--------"+o.getDepotId());
        }
        return JSON.toJSONString(depotInfos);
    }
    @RequestMapping("/updateInvenCheck")
    @ResponseBody
    public void updateInvenCheck(HttpServletRequest req) throws ParseException {
        String checkData=req.getParameter("checkData");
        String depId=req.getParameter("depId");
        String comment=req.getParameter("comment");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(checkData);
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(invId);
        int c=0;
        for(int i=0;i<inventoryCheckDescs.size();i++){
            Integer a= inventoryCheckDescs.get(i).getInvCheckDescCheckNum();
            c+=a;
        }
        if(count>0){
            InventoryCheck inventoryCheck = new InventoryCheck();
            inventoryCheck.setInvCheckId(invId);
            inventoryCheck.setInvCheckDate(parse);
            inventoryCheck.setDepotId(depId);
            inventoryCheck.setEmpId("admin");
            inventoryCheck.setInvCheckProBar(count);
            inventoryCheck.setInvCheckProNum(c);
            inventoryCheck.setInvCheckProComment(comment);
            int i = checkServiceInter.updateInvenCheck(inventoryCheck);
        }
    }

    /**
     * 修改库存数量
     */
    @RequestMapping("udpateInvenDescNum")
    @ResponseBody
    public void udpateInvenDescNum(){
        InventoryCheckDesc inventoryDesc = new InventoryCheckDesc();
        int i = checkServiceInter.updateInvenDescNum(inventoryDesc);
    }
}
